package com.lovo.test;

import java.util.InputMismatchException;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int[] computerArr = create();
        for (int items : computerArr) {
            System.out.println(items);
        }


        // 输入中奖数组

        for (int n = 0; n < 5; n++) {
            int[] luckyArr = new int[4];
            for (int i = 0; i < 4; i++) {
                while (true) {
                    try {
                        System.out.println("请输入" + (i + 1) + "个数");
                        luckyArr[i] = scan.nextInt();
                        for (int j = 0; j < i; j++) {
                            if (luckyArr[i] == luckyArr[j]) {
                                System.out.println("重复输入");
                                i--;
                            }
                        }
                        break;
                    } catch (InputMismatchException e) {
//                System.out.println(e.getMessage());
//                e.printStackTrace();
                        System.out.println("请重新输入");
                        scan.next();
                    }

                }
            }
            String resulet = a(luckyArr, computerArr);
            if (resulet.equals("公牛等于母牛")) {
                break;
            }


        }
    }

    //机选不重复函数
    public static int[] create() {
        int[] arr = new int[4];
        for (int i = 0; i < 4; i++) { // 找6次
            int num = (int) (Math.random() * 9 + 1); // 随机数
            boolean repeat = false; // 重复状态
            for (int j = 0; j < i; j++) {
                if (num == arr[j]) { // 重复
                    repeat = true;
                }
            }
            if (repeat) { // 重复
                i--;
            } else { // 不重复
                arr[i] = num;
            }
        }
        return arr;
    }


    //判断函数
    public static String a(int[] luckyArr, int[] computerArr) {
        int count = 0;
        int a = 0;


        for (int i = 0; i < luckyArr.length; i++) {
            if (luckyArr[i] == computerArr[i]) {
                a++;
            }
            for (int j = 0; j < computerArr.length; j++) {
                if (luckyArr[i] == computerArr[j]) {
                    count++;
                }
            }
        }
        int b = count - a;
        System.out.println(a + "A," + b + "B");
        if (a == 4) {
            return "公牛等于母牛";
        }

        return "不成功";
    }

}
